<template>
  <div class="comment-item">
    <img class="user-avatar" :src="userAvatar" @click="toUserInfo" />
    <div class="content">
      <div class="user">
        <span class="username" @click="toUserInfo">{{ username }}</span>
      </div>
      <p class="content-text">{{ commentInfo.content }}</p>
      <div class="info">
        <span class="time">{{ commentInfo.created_at | formatDate }}</span>
        <span class="like">
          <a href="javascript:" @click="thumbsUp"
            ><i
              :class="[clickState ? 'el-icon-liked' : 'el-icon-like']"
              ref="thumbs"
            ></i>
            {{ commentInfo.likes }}</a
          >
        </span>
        <span class="reply-btn" @click="showFatherInput">回复</span>
      </div>
      <div class="reply-box">
        <reply-list
          :cid="commentInfo.id"
          @show-son-input="showSonInput"
          ref="replyList"
        ></reply-list>
      </div>
      <reply-editor
        v-if="showFather"
        :cid="commentInfo.id"
        @reload-reply="reloadReply"
      ></reply-editor>
    </div>
  </div>
</template>

<script>
import { api } from "@/api";
import { StoreHandler } from "@/store";
import ReplyList from "./ReplyList.vue";
import ReplyEditor from "./editor/ReplyEditor.vue";
export default {
  components: { ReplyList, ReplyEditor },
  data() {
    return {
      username: "",
      clickState: false,
      showFather: false,
    };
  },
  props: ["commentInfo"],
  methods: {
    async getUsernameById(id) {
      this.username = (await this.$axios(api.user.getUsernameById(id))).data;
    },
    reloadReply() {
      this.$refs.replyList.loadReplyInfo(null, null, 1);
    },
    async thumbsUp() {
      if (this.clickState) {
        this.$notify.info({
          message: "只能点一次赞哦",
          offset: 60,
        });
        return;
      }
      await this.$axios(api.comment.thumbsUp(this.commentInfo.id));
      this.commentInfo.likes = this.commentInfo.likes + 1;
      this.clickState = true;
    },
    toUserInfo() {
      let userInfo = StoreHandler.getters.getUserInfo();
      if (userInfo) {
        this.$router.push({
          path: "/otherUser",
          query: { username: this.username },
        });
      } else {
        this.$router.push("/login");
      }
    },
    showSonInput() {
      this.showFather = false;
    },
    showFatherInput() {
      this.$refs.replyList.showSon = false;
      this.showFather = true;
    },
  },
  filters: {
    formatDate(input) {
      return new Date(input).format("yyyy-MM-dd hh:mm");
    },
  },
  computed: {
    userAvatar() {
      return (
        "/api/" + api.user.getAvatar(this.username).url + `?${Math.random()}`
      );
    },
  },
  mounted() {
    this.getUsernameById(this.commentInfo.uid);
  },
};
</script>

<style lang="less" scoped>
.comment-item {
  .user-avatar {
    position: absolute;
    margin-top: 20px;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    cursor: pointer;
  }
  .content {
    padding-top: 20px;
    margin-top: 20px;
    margin-left: 50px;
    border-top: 1px solid #e5e9ef;
    .content-text {
      margin-top: 5px;
    }
    .user {
      .username {
        color: #6d757a;
        font-weight: bold;
        cursor: pointer;
      }
    }
    .info {
      .time {
        font-size: 12px;
        color: #99a2aa;
        margin-right: 10px;
      }
      .like a {
        text-decoration: none;
        color: #99a2aa;
        font-size: 12px;
        margin-right: 30px;
      }
      .reply-btn {
        font-size: 12px;
        color: #99a2aa;
        cursor: pointer;
      }
    }
  }
}
</style>
